var offset=0;
var limit=30;
var pagingLimit=30;//显示分页时的条数
var currentTotalElements=0;//当前显示总条数
var search_companyName=null;
var isMainCompany=window.company.isMainCompany;
var deleteZhuancheType=window.deleteZhuancheType;
$(function(){
	$(document).on("click",".searchButton",function(){
		var p=	$(this).parent().parent().prev();
		var input=$(p).children('div').children('input');
		search_companyName=$(input).val();
		offset=0;
		var sss = $(this).parent().parent().parent().parent().parent().parent().parent()[0];
		panelHead($(sss).children("div:first-child")[0]);
	});
	initdata(false);
	$(document).on("keydown","body",function(){
		if(window.event.keyCode == 13){
			$(".searchButton").click();
		}
	});
	
	
});
$(document).on("mouseover",".panel-title",function(){
	$(this).css("backgroundColor","#8E8E8E");
});
$(document).on("mouseout ",".panel-title",function(){
	$(this).css("backgroundColor","");
});

$(document).on("click",".updateCarType_company",function(){
	var  carTypeName = $(this).attr('carTypeName');
	var  carTypeId = $(this).attr('carTypeId');
	var  companyId = $(this).attr('companyId');
	$.ajax({
        url : ctx+'/zhuanche/queryCarTypeCompany',
        type:'GET',
        async:false,
        dataType : 'json',
        data : {
        	"carTypeId":carTypeId,
        	"companyId":companyId,
        	"pager.offset":offset,
        	"limit":limit,
        },
        success : function(result){
        	
        	
        	var data = result.page.content[0];
        	
        	$("#carTypeName_update").text(carTypeName);
        	$("#companyName_update").text(data.companyName);
        	$("#carTypeId_update").val(carTypeId);
        	$("#companyId_update").val(companyId);
        	
        	var tangzuAreaId = data.tangzuAreaId;
        	var rizuAreaId = data.rizuAreaId;
        	var banrizuAreaId = data.banrizuAreaId;
        	var jiejiAreaId = data.jiejiAreaId;
        	var songjiAreaId = data.songjiAreaId;
        	var jiezhanAreaId = data.jiezhanAreaId;
        	var songzhanAreaId = data.songzhanAreaId;
        	
        	var tangzuAreaName = data.tangzuAreaName;
        	var rizuAreaName = data.rizuAreaName;
        	var banrizuAreaName = data.banrizuAreaName;
        	var jiejiAreaName = data.jiejiAreaName;
        	var songjiAreaName = data.songjiAreaName;
        	var jiezhanAreaName = data.jiezhanAreaName;
        	var songzhanAreaName = data.songzhanAreaName;
        	//生成各个收费标准的option，并选中值
        	var tangzuAreaSelect =  getAreaContentSelected(result,tangzuAreaId);
        	$("#tangzuAreaId_company_update").html(tangzuAreaSelect).chosen().change(function(){});
        	$("#tangzuAreaId_company_update option").each(function(index,element){
        		var flag = false;
                if($(element).attr("selected") == "selected"){
                	$("#tangzuAreaId_company_update").next().find("a").text(tangzuAreaName);
                	return false;
                }
                if(!flag){
                	$("#tangzuAreaId_company_update").next().find("a").html("<em>请选择收费标准</em>");
                }
            });
        	
        	var rizuAreaSelect =  getAreaContentSelected(result,rizuAreaId);
        	$("#rizuAreaId_company_update").html(rizuAreaSelect).chosen().change(function(){});
        	$("#rizuAreaId_company_update option").each(function(index,element){
        		var flag = false;
                if($(element).attr("selected") == "selected"){
                	$("#rizuAreaId_company_update").next().find("a").text(rizuAreaName);
                	return false;
               }
                if(!flag){
                	$("#rizuAreaId_company_update").next().find("a").html("<em>请选择收费标准</em>");
                }
            });
        	
        	var banrizuAreaSelect =  getAreaContentSelected(result,banrizuAreaId);
        	$("#banrizuAreaId_company_update").html(banrizuAreaSelect).chosen().change(function(){});
        	$("#banrizuAreaId_company_update option").each(function(index,element){
        		var flag = false;
                if($(element).attr("selected") == "selected"){
                	$("#banrizuAreaId_company_update").next().find("a").text(banrizuAreaName);
                	return false;
               }
                if(!flag){
                	$("#banrizuAreaId_company_update").next().find("a").html("<em>请选择收费标准</em>");
                }
            });
        	
        	var jiejiAreaSelect =  getAreaContentSelected(result,jiejiAreaId);
        	$("#jiejiAreaId_company_update").html(jiejiAreaSelect).chosen().change(function(){});
        	$("#jiejiAreaId_company_update option").each(function(index,element){
        		var flag = false;
                if($(element).attr("selected") == "selected"){
                	$("#jiejiAreaId_company_update").next().find("a").text(jiejiAreaName);
                	return false;
               }
                if(!flag){
                	$("#jiejiAreaId_company_update").next().find("a").html("<em>请选择收费标准</em>");
                }
            });
        	
        	var songjiAreaSelect =  getAreaContentSelected(result,songjiAreaId);
        	$("#songjiAreaId_company_update").html(songjiAreaSelect).chosen().change(function(){});
        	$("#songjiAreaId_company_update option").each(function(index,element){
        		var flag = false;
                if($(element).attr("selected") == "selected"){
                	$("#songjiAreaId_company_update").next().find("a").text(songjiAreaName);
                	return false;
               }
                if(!flag){
                	$("#songjiAreaId_company_update").next().find("a").html("<em>请选择收费标准</em>");
                }
            });
        	
        	var jiezhanAreaSelect =  getAreaContentSelected(result,jiezhanAreaId);
        	$("#jiezhanAreaId_company_update").html(jiezhanAreaSelect).chosen().change(function(){});
        	$("#jiezhanAreaId_company_update option").each(function(index,element){
        		var flag = false;
                if($(element).attr("selected") == "selected"){
                	$("#jiezhanAreaId_company_update").next().find("a").text(jiezhanAreaName);
                	return false;
               }
                if(!flag){
                	$("#jiezhanAreaId_company_update").next().find("a").html("<em>请选择收费标准</em>");
                }
            });
        	
        	var songzhanAreaSelect =  getAreaContentSelected(result,songzhanAreaId);
        	$("#songzhanAreaId_company_update").html(songzhanAreaSelect).chosen().change(function(){});
        	$("#songzhanAreaId_company_update option").each(function(index,element){
        		var flag = false;
                if($(element).attr("selected") == "selected"){
                	$("#songzhanAreaId_company_update").next().find("a").text(songzhanAreaName);
                	return false;
               }
                if(!flag){
                	$("#songzhanAreaId_company_update").next().find("a").html("<em>请选择收费标准</em>");
                }
            });
        }
	});    
	
	//收费标准下拉框选值变化，改变下拉框显示值
	$(document).on("click",".active-result",function(){
		$($(this).parent().parent().parent().children()[0]).html($(this).text())
	});
	
	$('#dialog_carTypeCompany_update').modal();
});
function panelHead (obj) {
	var carTypeId=$(obj).attr("carTypeId");
	var carTypeName=$(obj).attr("typeName");
	var carTypeCompanyTbody=$(obj).parent().parent().children('div').children('div').children('div').children('div').children('table').children('tbody');
	var ul=$(obj).parent().parent().children('div').children('div').children('div').children('div').children('ul');
	var pageshow=$(ul[0]);
	var pagedata=$(ul[1]);
	$.ajax({
        url : ctx+'/zhuanche/queryCarTypeCompany',
        type:'GET',
        async:false,
        dataType : 'json',
        data : {
        	"carTypeId":carTypeId,
        	"companyName":search_companyName,
        	"pager.offset":offset,
        	"limit":limit,
        },
        success : function(result){
        	var newcontent = '';
        	var totalElements=result.page.totalElements;
        	if(totalElements>0){
        		$(result.page.content).each(function(index,data){
        		
        		var tangzuAreaName = (data.tangzuAreaName)== null ? "--" :(data.tangzuAreaName) ;
        		var rizuAreaName = (data.rizuAreaName)== null ? "--" :(data.rizuAreaName) ;
        		var banrizuAreaName = (data.banrizuAreaName)== null ? "--" :(data.banrizuAreaName) ;
        		var jiejiAreaName = (data.jiejiAreaName)== null ? "--" :(data.jiejiAreaName) ;
        		var songjiAreaName = (data.songjiAreaName)== null ? "--" :(data.songjiAreaName) ;
        		var jiezhanAreaName = (data.jiezhanAreaName)== null ? "--" :(data.jiezhanAreaName) ;
        		var songzhanAreaName = (data.songzhanAreaName)== null ? "--" :(data.songzhanAreaName) ;
        		
        			
				var updateButton=
					"<div class='pull-right'><a href='#' class='btn btn-primary btn-xs updateCarType_company' " +
					"carTypeId='"+carTypeId+"' companyId='"+data.companyId+"' carTypeName='"+carTypeName+"'>编辑</a>";
				var deleteButton = "&emsp;<a href='#'  data-value='"+data.id+"'  class='btn btn-primary  btn-xs deleteTypeCompany'>删除</a></div>";
			newcontent+="<tr>"+
	    				"<td>"+data.companyName+"</td>"+
						"<td>"+tangzuAreaName+"</td>"+
						"<td>"+rizuAreaName+"</td>"+
						"<td>"+banrizuAreaName+"</td>"+
						"<td>"+jiejiAreaName+"</td>"+
						"<td>"+songjiAreaName+"</td>"+
						"<td>"+jiezhanAreaName+"</td>"+
						"<td>"+songzhanAreaName+"</td>"+
						"<td>"+updateButton+deleteButton+"</td>"+
					"</tr>";
        		});
        	}
        	$(carTypeCompanyTbody).html(newcontent);
        	firstLoadPages=true;
        	$(".pageshow"+$(pageshow).attr("data-index")+"").html("<li><a>共"+totalElements+"条数据</a></li>");
    		$(".pagedata"+$(pagedata).attr("data-index")+"").paging(totalElements, {
    			format: '[< ncnnn >]',
    			perpage: pagingLimit,
    			page:(offset/limit)+1,
    			onSelect: function (page) {
    				limit=pagingLimit;
    				offset=(page-1)*limit;
    				if(!firstLoadPages){
    					panelHead(obj);
    				}
    				firstLoadPages = false;
    		    	return false;
    			},
    			onFormat: function (type) {
    				switch (type) {
    				case 'block': // n and c
    					if (!this.active) {
    						return '';
    					} else if (this.value != this.page) {
    						return '<li><a href="javascript:;">' + this.value + '</a></li>';
    					}
    					return '<li class="active"><a href="javascript:;">' + this.value + '</a></li>';
    				case 'next': // >
    					return '<li><a href="javascript:;">&rsaquo;</a><li>';
    				case 'prev': // <
    						return '<li><a href="javascript:;">&lsaquo;</a><li>';
    				case 'first': // [
    						return '<li><a href="javascript:;">&laquo;</a><li>';
    				case 'last': // ]
    						return '<li><a href="javascript:;">&raquo;</a></li>';
    				}
    			}
    		});
        	
        }
	});
	search_companyName="";
}
$(document).on("click",".panel-heading",function(){
	panelHead(this);
});
function initdata(isappend){
	$.ajax({
        url : ctx+'/zhuanche/queryCarTypeV4up',
        type:'GET',
        async:false,
        dataType : 'json',
        data : {
        	"pager.offset":offset,
        	"limit":limit,
        },
        success : function(result){
        	//显示全部收费标准
        	var areaContent =getAreaContent(result);
        	$("#tangzuAreaId_company").html(areaContent).chosen().change(function(){});
        	$("#rizuAreaId_company").html(areaContent).chosen().change(function(){});
        	$("#banrizuAreaId_company").html(areaContent).chosen().change(function(){});
        	$("#jiejiAreaId_company").html(areaContent).chosen().change(function(){});
        	$("#songjiAreaId_company").html(areaContent).chosen().change(function(){});
        	$("#jiezhanAreaId_company").html(areaContent).chosen().change(function(){});
        	$("#songzhanAreaId_company").html(areaContent).chosen().change(function(){});
        	
        	
        	var newcontent='';
        	var accessibleTypeDiv  = '';//童俊超添加新增选择可接单类型
        	var accessibleTypeDiv2  = '';//童俊超添加修改选择可接单类型
        	var totalElements=result.page.totalElements;
        	if(totalElements>0){
        		$(result.page.content).each(function(index,data){
        			var ifTaxi="否";
        			if (data.ifTaxi=="1") {  //是出租车
        				ifTaxi="是";
        			}
        			newcontent+="<div class='panel-group' id='accordion"+index+"'  role='tablist' aria-multiselectable='true'>"+
        			  "<div class='panel panel-default'>"+
        				"<div class='panel-heading' carTypeId='"+data.id+"' typeName='"+data.typeName+"' role='tab' id='heading"+index+"'>"+
        				  "<div class='panel-title'>"+
        					"<a data-toggle='collapse' data-parent='#accordion"+index+"' href='#collapse"+index+"' aria-expanded='true' aria-controls='collapse"+index+"'>"+
        					//"<div class='col-md-3'>"+data.typeName+"</div>  <div class='col-md-3'>"+ifTaxi+" </div> <div class='col-md-3'>"+data.sequence+"</div>"+
        					"<div class='col-md-5'>"+data.typeName+"</div> <div class='col-md-5'>"+data.sequence+"</div>"+
        					"</a>"+
        					"<div class='pull-right'>"+
	        					"<button href='#' cartypeId='"+data.id+"' class='btn btn-primary btn-xs updateCarType' typeName='"+data.typeName+"' sequence='"+data.sequence+"' picture='"+data.picture+"' ifTaxi ='"+data.ifTaxi +"' accessibleType='"+data.accessibleType+"' >编辑</button>&emsp;";
	        					if(deleteZhuancheType){
	        						newcontent += "<button href='#'  data-value='"+data.id+"'  class='btn btn-primary btn-xs deletethis'>删除</button>&emsp;";
	        					}
	        					newcontent+="<button class='btn btn-primary btn-xs startService' cartypeId='"+data.id+"' typeName='"+data.typeName+"'>开通车型</button>"+
        					"</div>"+
        					"</div>"+
        				"</div>"+
        				"<div id='collapse"+index+"' class='panel-collapse collapse ' role='tabpanel' aria-labelledby='heading"+index+"'>";
        				if(isMainCompany){
					newcontent+="<div class='tab-content'>"+
			        				"<div class='container-fluid'>"+
			        			  		"<div class='row query-pad'>"+
			        			  			"<div class='col-xs-11' style='padding: 0px;'>"+
			        			  				"<div class='col-xs-4 input-query'>"+
				        			  				"<span>公司名称:</span>"+
				        			  				"<input type='text' class='form-control form-contb' id='companyName' name='companyName' placeholder='请输入公司名字'/>"+
			        			  				"</div>"+
		        			  				 "</div>"+
		        			  				 "<div class='col-xs-1' style='padding: 0px;'>"+
		        			  				 	"<div class='input-query-bttom'>"+
		        			  				 		"<button class='btn btn-primary btn-xs searchButton'>&emsp;查询&emsp;</button>"+
	        			  				 		"</div>"+
        			  				 		 "</div>"+
			        			  		"</div>"+
			        			  	 "</div>"+
		        			  	"</div>";
        				}
        			
        				newcontent+= "<div class='panel-body'>"+
        					"<div>"+
        						"<table class='table table-condensed table-striped'>"+
        							"<thead>"+
        								"<tr>"+
        									"<th class='col-xs-2' style='width:16%'>公司名称</th>"+
        									"<th class='col-xs-1' style='width:11%'>立即用车收费</th>"+
        									"<th class='col-xs-1' style='width:11%'>日租收费</th>"+
        									"<th class='col-xs-1' style='width:11%'>半日租收费</th>"+
        									"<th class='col-xs-1' style='width:11%'>接机收费</th>"+
        									"<th class='col-xs-1' style='width:11%'>送机收费</th>"+
        									"<th class='col-xs-1' style='width:11%'>接站收费</th>"+
        									"<th class='col-xs-1' style='width:11%'>送站收费</th>"+
        									"<th class='col-xs-2' style='width:10%'>操作</th>"+
        								"</tr>"+
        							"</thead>"+
        							"<tbody>"+
        							"</tbody>"+
        						"</table>"+
        						"<ul class='pagination pull-left pagination-sm pageshow"+index+"' data-index='"+index+"'><li><a>共0条数据</a></li></ul>"+
        			        	"<ul class='pagination pull-left pagination-sm pagedata"+index+"' data-index='"+index+"'></ul>"+
        			        	
        					"</div>"+
        				  "</div>"+
        				"</div>"+
        			  "</div>"+
        			"</div>";
        		//童俊超添加选择可接单类型
	        	accessibleTypeDiv += "<input type='checkbox' name='accessibleType' value='"+data.id+"'> " 
	        				   + "<span class='checkbox-margin'>"+data.typeName+"</span>&emsp;";
	        	
	        	accessibleTypeDiv2 += "<div id='accessible"+data.id+"'style='float:left'>" +
	        			"<input type='checkbox' name='accessibleType2' value='"+data.id+"'> " +"<span class='checkbox-margin'>"+data.typeName+"</span>&emsp;" + "</div>";
        		});
        	}
        	//童俊超添加选择可接单类型
        	$("#accessibleTypeDiv").append(accessibleTypeDiv);
        	$("#accessibleTypeDiv2").append(accessibleTypeDiv2);
        	$("#carTypeList").append(newcontent);
    		$("#carTypetotal").html("<a>共"+totalElements+" 条数据</a>");
        }
	});
};

function getAreaContent(result){
	var areaContent = '';
	var areaList = result.areaList;
	$(areaList).each(function(index,data){
		areaContent+="<option value='"+data.id+"'>"+data.name+"</option>"
	});
	return areaContent;
};
function getAreaContentSelected(result,id){
	
	var areaContent = '';
	var areaList = result.areaList;
	$(areaList).each(function(index,data){
		if(id == data.id ){
			areaContent+="<option value='"+data.id+"' selected='selected'>"+data.name+"</option>"
		}else{
			areaContent+="<option value='"+data.id+"'>"+data.name+"</option>"
		}
		
	});
	return areaContent;
};

